<?php 
    use Zend\Escaper\Escaper;    
?>

<script> 
    <?php
        // Countries and states are stored in JSON objects in order to be used
        // to fill address country and states selects
        echo sprintf('var countries = %s;', $this->json($this->countries));
        echo sprintf('var states = %s;', $this->json($this->states));
    ?>

    $(document).ready(function() {  
        <?php 
            echo sprintf('$(\'#%s_country\').change(function() { reloadStates($(\'#%s_country\').val()); });', $this->type, $this->type);
            echo sprintf('reloadStates($(\'#%s_country\').val());', $this->type);            
        ?>
    });

    function reloadStates(countryId) {
        <?php echo sprintf('var statesSelect = $(\'#%s_state\');', $this->type); ?>

        statesSelect.find('option').remove();
        
        Enumerable.From(states).Where(function(state) {
                return state.countryId == countryId;
        	}).ForEach(function(state) {
        		statesSelect.append($("<option />").val(state.id).text(state.name));
            });
    };

    function setSelected(selector, value) 
    {
    	$(selector).val(value);
    };
    
    function validate() {
        <?php echo sprintf('var type = \'%s\';', $this->type);  ?>

        // Hide all validators
        $('.validator').css('visibility', 'hidden');
        
        // Check field function
        var checkField = function(name) {
            var fieldValue = $('[name="' + type + '_' + name + '"]').val();
            
            if (fieldValue == '' || fieldValue == null) {
            	$('#' + type + '_' + name + '_validator').css('visibility', 'visible');
            	
                return false;
            }
            
            return true;
        }

        var isValid = true;

        isValid &= checkField('street'); 
        isValid &= checkField('number'); 
        isValid &= checkField('city'); 
        isValid &= checkField('zip_code'); 
        isValid &= checkField('state_id'); 
        isValid &= checkField('country_id'); 
        
        return isValid;
    };
</script>
    <?php 
        $street = '';
        $number = '';
        $city = '';
        $state = '';
        $country = '';
        $stateId = null;
        $countryId = null;
        $zipCode = '';
        
        // Retrieve address values in order to fill address form if user address is not null
        if (!is_null($this->address)) {
            $street = $this->address->getStreet();
            $number = $this->address->getNumber();
            $city = $this->address->getCity();
            $state = $this->address->getState();
            $country = $this->address->getCountry();
            $stateId = $this->address->getStateId();
            $countryId = $this->address->getCountryId();
            $zipCode = $this->address->getZipCode();
        }
        
        // Rendering the form
        $escaper = new Escaper('utf-8');
        
        echo sprintf('<div>%s</div>', $this->translate('insert your address', 'user', $this->locale));
        
        echo sprintf('<div>%s</div>', $this->translate('street', 'user', $this->locale));
        echo sprintf('<input type="text" class="address" name="%s_street" value="%s"></input>', $this->type, $escaper->escapeHtml($street));
        echo sprintf('<div class="validator" id="%s_street_validator">%s</div>', $this->type, $this->translate('fill street', 'user', $this->locale));
        
        echo sprintf('<div>%s</div>', $this->translate('number', 'user', $this->locale));
        echo sprintf('<input type="text" class="address" name="%s_number" value="%s"></input>', $this->type, $escaper->escapeHtml($number));
        echo sprintf('<div class="validator" id="%s_number_validator">%s</div>', $this->type, $this->translate('fill number', 'user', $this->locale));
        
        echo sprintf('<div>%s</div>', $this->translate('city', 'user', $this->locale));
        echo sprintf('<input type="text" class="address" name="%s_city" value="%s"></input>', $this->type, $escaper->escapeHtml($city));
        echo sprintf('<div class="validator" id="%s_city_validator">%s</div>', $this->type, $this->translate('fill city', 'user', $this->locale));

        echo sprintf('<div>%s</div>', $this->translate('zip code', 'user', $this->locale));
        echo sprintf('<input type="text" class="address" name="%s_zip_code" value="%s"></input>', $this->type, $escaper->escapeHtml($zipCode));
        echo sprintf('<div class="validator" id="%s_zip_code_validator">%s</div>', $this->type, $this->translate('fill zip code', 'user', $this->locale));
        
        echo sprintf('<div>%s</div>', $this->translate('state', 'user', $this->locale));
        echo sprintf('<select class="address" id="%s_state" name="%s_state_id"></select>', $this->type, $this->type);
        echo sprintf('<div class="validator" id="%s_state_id_validator">%s</div>', $this->type, $this->translate('fill state', 'user', $this->locale));
        
        echo sprintf('<div>%s</div>', $this->translate('country', 'user', $this->locale));
        echo sprintf('<select class="address" id="%s_country" name="%s_country_id">', $this->type, $this->type);
        
        foreach ($countries as $country)
            echo sprintf('<option value="%s">%s</option>', $country->id, $escaper->escapeHtml($country->name));
            
        echo sprintf('</select>');
        echo sprintf('<div class="validator" id="%s_country_id_validator">%s</div>', $this->type, $this->translate('fill country', 'user', $this->locale));
        
        if (!is_null($countryId))
            echo sprintf('<script type="text/javascript">$( document ).ready(function() { setSelected(\'#%s_country\', %s); });</script>', $this->type, $countryId);

        if (!is_null($stateId))
            echo sprintf('<script type="text/javascript">$( document ).ready(function() { setSelected(\'#%s_state\', %s); });</script>', $this->type, $stateId);
    ?>
